package org.budo.dubbo.protocol.http.view.render.budo;

import org.budo.support.assertion.AssertArrayNotEmptyException;
import org.budo.support.assertion.AssertCollectionNotEmptyException;
import org.budo.support.assertion.AssertException;
import org.budo.support.assertion.AssertMapNotEmptyException;
import org.budo.support.assertion.AssertNotNullException;
import org.budo.support.assertion.AssertStringNotEmptyException;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * @author lmw
 */
public class AssertExceptionRender {
    private static final Logger log = Slf4j.getLogger();

    public ApiError buildApiError(AssertException exception) {
        if (exception instanceof AssertNotNullException) {
            return ApiError.ASSERT_NOT_NULL_FAILED.withDescription(exception.getMessage());
        }

        if (exception instanceof AssertStringNotEmptyException) {
            return ApiError.ASSERT_STRING_NOT_EMPTY_FAILED.withDescription(exception.getMessage());
        }

        if (exception instanceof AssertCollectionNotEmptyException) {
            return ApiError.ASSERT_COLLECTION_NOT_EMPTY_FAILED.withDescription(exception.getMessage());
        }

        if (exception instanceof AssertMapNotEmptyException) {
            return ApiError.ASSERT_MAP_NOT_EMPTY_FAILED.withDescription(exception.getMessage());
        }

        if (exception instanceof AssertArrayNotEmptyException) {
            return ApiError.ASSERT_ARRAY_NOT_EMPTY_FAILED.withDescription(exception.getMessage());
        }

        log.error("#39 renderException other assert fail, exception=" + exception, exception);
        return ApiError.ASSERT_FAILED;
    }
}